package com.kaka.bus.engine.borrow.person.step;

import android.support.v7.widget.AppCompatButton;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.kaka.bus.R;
import com.kaka.bus.base.fragment.SimpleFragment;
import com.kaka.bus.constant.UserConstant;
import com.kaka.bus.engine.borrow.person.PersonActivity;
import com.kaka.bus.manager.BottomDialogManager;
import com.kaka.bus.util.U;

import java.util.ArrayList;
import java.util.LinkedHashMap;

import butterknife.BindView;

/**
 * Created by zhangqi on 2017/12/21.
 * e-mail : ${email}
 * desc :
 */

public class Step3Fragment extends SimpleFragment {

    @BindView(R.id.tv_shehui_parent)
    TextView mTvShehuiParent;
    @BindView(R.id.tv_shehui_pcontact)
    TextView mTvShehuiPcontact;
    @BindView(R.id.tv_shehui_friend)
    TextView mTvShehuiFriend;
    @BindView(R.id.tv_shehui_fcontact)
    TextView mTvShehuiFcontact;
    @BindView(R.id.btn_shehui_commit)
    AppCompatButton mBtnShehuiCommit;

    private String mParentName;
    private String mFriendName;
    private LinkedHashMap<String,String> mSheHuiMap;
    private ArrayList<String> mParentList;
    private ArrayList<String> mFriendList;

    private BottomDialogManager mParentManager;
    private BottomDialogManager mFriendManager;
    private PersonActivity mPersonActivity;

    private static final int PARENT = 100;
    private static final int FRIEND = 101;

    @Override
    protected void initView(View view) {
        super.initView(view);
        mPersonActivity = (PersonActivity) getActivity();
        mFriendManager = new BottomDialogManager(mActivity);
        mParentManager = new BottomDialogManager(mActivity);
    }

    @Override
    protected void initData() {

        if(mSheHuiMap == null){
            mSheHuiMap = new LinkedHashMap<>();
        }

        /**
         * 亲属相关参数的初始化
         */
        if(mParentList == null) {
            mParentList = new ArrayList<>();
            mParentList.add("父母");
            mParentList.add("夫妻");
            mParentList.add("兄弟姐妹");
            mParentList.add("子女");
            mParentManager.setData(mParentList);
        }

        /**
         * 朋友相关参数的初始化
         */
        if(mFriendList == null){
            mFriendList = new ArrayList<>();
            mFriendList.add("同事");
            mFriendList.add("同学");
            mFriendList.add("朋友");
            mFriendManager.setData(mFriendList);
        }
    }

    @Override
    protected void initListener() {

        mTvShehuiParent.setOnClickListener(v -> mParentManager.show());
        mTvShehuiFriend.setOnClickListener(v -> mFriendManager.show());

        mParentManager.setOkListener(select -> mTvShehuiParent.setText(select));
        mFriendManager.setOkListener(select -> mTvShehuiFriend.setText(select));

        mTvShehuiFcontact.setOnClickListener(v -> {
            /**
             * 去选择通讯录
             */
            mPersonActivity.selectContact(FRIEND);
        });

        mTvShehuiPcontact.setOnClickListener(v -> {
            mPersonActivity.selectContact(PARENT);
        });

        mBtnShehuiCommit.setOnClickListener(v -> {
            /**
             * 提交参数
             */
            if(check()){
                mPersonActivity.stepContinue(2);
            }
        });
    }

    private boolean check() {

        String parent = getText(mTvShehuiParent);
        String pContact = getText(mTvShehuiPcontact);
        String friend = getText(mTvShehuiFriend);
        String fContact = getText(mTvShehuiFcontact);

        if(TextUtils.isEmpty(parent)){
            showMessage("请选择亲属关系!");
            return false;
        }

        if (TextUtils.isEmpty(pContact)){
            showMessage("请选择亲属关系联系人!");
            return false;
        }

        if(TextUtils.isEmpty(friend)){
            showMessage("请选择社会关系!");
            return false;
        }

        if(TextUtils.isEmpty(pContact)){
            showMessage("请选择社会关系联系人!");
            return false;
        }

        //1.0.2bug mFriendName为空
        if(TextUtils.isEmpty(mFriendName)){
            showMessage("请重新选择联系人!");
            return false;
        }

        if(mFriendName.equals(mParentName)){
            showMessage("亲属与社会关系的联系人重复!");
            return false;
        }

        switch (parent){
            case "夫妻":
                parent = "0";
                break;
            case "父母":
                parent = "1";
                break;
            case "兄弟姐妹":
                parent = "2";
                break;
            case "子女":
                parent = "3";
                break;
        }
        mSheHuiMap.put(UserConstant.RELATIVES,parent);
        switch (friend){
            case "同事":
                friend = "4";
                break;
            case "同学":
                friend = "5";
                break;
            case "朋友":
                friend = "6";
                break;
        }
        mSheHuiMap.put(UserConstant.SOCIETY,friend);

        mSheHuiMap.put(UserConstant.RELA_PHONE,mParentName);
        mSheHuiMap.put(UserConstant.RELATIVES_NAME,pContact);
        mSheHuiMap.put(UserConstant.SOCI_PHONE,mFriendName);
        mSheHuiMap.put(UserConstant.SOCIETY_NAME,fContact);

        mPersonActivity.mPMap.putAll(mSheHuiMap);
        return true;
    }

    public void setPhoneAndName(int code, String phone, String username) {
        phone = phone.replaceAll(" ", "").replaceAll("-", "").replace("+86","");
        if(phone.startsWith("86")){
            phone = phone.replaceAll("86","");
        }

        /**
         * 不能选择号码低于7 并且高于14的 号码
         */
        if(!U.isMobileExact(phone)){
            showMessage("您选择的必须是11位手机号!");
            return;
        }

        /**
         * 联系人名字必须是汉字
         */
        if(!U.isChz(username)){
            showMessage("所选取的联系人的姓名必须是汉字");
            return;
        }

        if(code == PARENT){
            mTvShehuiPcontact.setText(username);
            mParentName = phone;
        }else if(code == FRIEND){
            mTvShehuiFcontact.setText(username);
            mFriendName = phone;
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_shehui;
    }

}
